package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Play;
import com.jcf.springboot.entity.business.vo.PlayVo;
import com.jcf.springboot.service.PlayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-04-03
 */
@CrossOrigin
@RestController
@RequestMapping("/filmplay")
public class PlayController {

    @Autowired
    private PlayService playService;

    @GetMapping("/getList")
    public R getList(){
        QueryWrapper<Play> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("play_open_time");
        List<Play> list = playService.list(wrapper);
        if(list.size() > 0){
           return R.ok().data(list).message("获取放映列表成功");
        }else {
          return R.error().message("获取放映列表失败");
        }
    }



    @GetMapping("/getPlayById")
    public R getPlayById(String id){
        Play play = playService.getById(id);
        if(play != null){
            return R.ok().data(play).message("获取放映信息成功");
        }
        else {
            return R.error().message("获取放映信息失败");
        }
    }


    @GetMapping("/getListByFilmId")
    public R getListByFilmId(String id){
        QueryWrapper<Play> playQueryWrapper = new QueryWrapper<>();
        playQueryWrapper.eq("film_id", id);
        List<Play> list = playService.list(playQueryWrapper);
        if(list != null){
            return R.ok().data(list).message("获取放映列表成功");
        }else {
            return R.error().message("获取放映列表失败");
        }
    }

    @PostMapping("/addPlay")
    public R addPlay(@RequestBody Play play){
        List<Play> list = playService.list(null);
        SimpleDateFormat f = new SimpleDateFormat("MM 月 dd 日 HH 点 mm 分");
        for (Play play1 : list) {
            if(play1.getPlayOpenTime().compareTo(play.getPlayOpenTime()) >= 0 && play1.getPlayOpenTime().compareTo(play.getPlayFinishTime())<=0){
                return R.error().message("时间冲突，请将时间设置在"+f.format(play1.getPlayOpenTime())+"之前");
            }
        }
        boolean save = playService.save(play);
        return R.ok().message("添加放映任务成功");
    }

    @GetMapping("getTotleList")
    public R getTotleList(String query,Integer pagenum,Integer pagesize){
        Page<PlayVo> page = new Page<>(pagenum, pagesize);
        List<PlayVo> totleList = playService.getTotleList(page);
        long total = page.getTotal();
        List<PlayVo> list = page.getRecords();
        Map<Object, Object> map = new HashMap<>();
        map.put("value",totleList);
        map.put("total", total);
        if(list != null){
            return R.ok().data(map).message("获取完整放映信息成功");
        }
        else{
            return R.error().message("获取完整放映信息失败");
        }
    }

    @DeleteMapping("removePlay")
    public R removePlay(String id){
        boolean b = playService.removeById(id);
        if(b){
            return R.ok().message("删除放映信息成功");
        }
        else{
            return R.error().message("删除放映信息失败");
        }
    }

    @PostMapping("upload")
    public R uploadExcel(MultipartFile file) throws IOException {
        playService.upload(file.getInputStream());

        return null;
    }

}

